﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

/// <summary>
/// sPage 的摘要说明
/// </summary>

    public  static class PageHelper
    {

        public static string fy(int totalCount, int pagesize, int page, string uri)
        {
            StringBuilder sb = new StringBuilder();
            int _pageCount = 0;
            int _len = 10;
            if (totalCount % pagesize == 0)
            {
                _pageCount = totalCount / pagesize;
            }
            else
            {
                _pageCount = totalCount / pagesize + 1;
            }
            if (page < 1)
                page = 1;
            if (page > _pageCount)
                page = _pageCount;
            //取得处理后的当前面URL
            string cutUrl = urlPage(uri);

            sb.Append("<ul id=\"nobr\">");
            if (page > 1)
                sb.Append("<li><a href=\"" + cutUrl + "page=" + (page - 1) + "\">&lt;&lt;&nbsp;</a></li>");
            else
                sb.Append("<li class=\"news_next_disabled\">&lt;&lt; &nbsp;</li>");
            int s, e;
            ten(_len, page, _pageCount, out s, out e);
            if (s > 3)
                sb.Append("<li><a href=\"" + cutUrl + "page=" + (page - _len) + "\">...</a></li>");
            for (; s < e; s++)
            {
                if (page == (s + 1))
                    sb.Append("<li class=\"news_next_on\"><a href=\"" + cutUrl + "page=" + (s + 1) + "\">" + (s + 1) + "</a></li>");
                else
                    sb.Append("<li><a href=\"" + cutUrl + "page=" + (s + 1) + "\">" + (s + 1) + "</a></li>");
            }
            if (_pageCount - e > 3)
                sb.Append("<li><a href=\"" + cutUrl + "page=" + (page + _len) + "\">...</a></li>");


            if (page < _pageCount)
                sb.Append("<li><a href=\"" + cutUrl + "page=" + (page + 1) + "\"> &nbsp;&gt;&gt;</a></li>");
            else
                sb.Append("<li class=\"news_next_disabled\"> &nbsp;&gt;&gt;</li>");
            sb.Append("</ul>");
            return sb.ToString();
        }

        public static string ShowPage(int cutPage, int totalCount, int pageSize, string uri)
        {
            string rel = "";
            int _firstPage = 0, _forwardPage = 0, _nextPage = 0, _endPage = 0, _totalPage = 0;
            if (totalCount > 0)
            {
                if ((totalCount % pageSize) == 0)
                {
                    _totalPage = totalCount / pageSize;
                }
                else
                {
                    _totalPage = (int)(totalCount / pageSize) + 1;
                }
            }
            if (cutPage > _totalPage)
            {
                cutPage = _totalPage;
            }
            _firstPage = 1;
            if (cutPage > 1)
            {
                _forwardPage = cutPage - 1;
            }
            else
            {
                _forwardPage = 1;
            }
            if ((cutPage + 1) < _totalPage)
            {
                _nextPage = cutPage + 1;
            }
            else
            {
                _nextPage = _totalPage;
            }
            _endPage = _totalPage;
            //取得处理后的当前面URL
            string cutUrl = urlPage(uri);

            if (cutPage > 1)
            {
                rel += "<a href='" + cutUrl + "page=1'>首页</a>&nbsp;";
            }
            else
            {
                rel += "首页&nbsp;";
            }
            if (cutPage > _forwardPage)
            {
                rel += "<a href='" + cutUrl + "page=" + _forwardPage + "'>上一页</a>&nbsp;";
            }
            else
            {
                rel += "上一页&nbsp;";
            }
            if (cutPage < _nextPage)
            {
                rel += "<a href='" + cutUrl + "page=" + _nextPage + "'>下一页</a>&nbsp;";
            }
            else
            {
                rel += "上一页&nbsp;";
            }
            if (cutPage < _endPage)
            {
                rel += "<a href='" + cutUrl + "page=" + _endPage + "'>最后一页</a>&nbsp;";
            }
            else
            {
                rel += "最后一页&nbsp;";
            }
            rel += "第" + cutPage + "页/总" + _totalPage + "页";
            return rel;
        }


        public static string ShowEnPage(int cutPage, int totalCount, int pageSize, string uri)
        {
            string rel = "";
            int _firstPage = 0, _forwardPage = 0, _nextPage = 0, _endPage = 0, _totalPage = 0;
            if (totalCount > 0)
            {
                if ((totalCount % pageSize) == 0)
                {
                    _totalPage = totalCount / pageSize;
                }
                else
                {
                    _totalPage = (int)(totalCount / pageSize) + 1;
                }
            }
            if (cutPage > _totalPage)
            {
                cutPage = _totalPage;
            }
            _firstPage = 1;
            if (cutPage > 1)
            {
                _forwardPage = cutPage - 1;
            }
            else
            {
                _forwardPage = 1;
            }
            if ((cutPage + 1) < _totalPage)
            {
                _nextPage = cutPage + 1;
            }
            else
            {
                _nextPage = _totalPage;
            }
            _endPage = _totalPage;
            //取得处理后的当前面URL
            string cutUrl = urlPage(uri);

            if (cutPage > 1)
            {
                rel += "<a href='" + cutUrl + "page=1'>|&lt; </a>&nbsp;";
            }
            else
            {
                rel += "|&lt; &nbsp;";
            }
            if (cutPage > _forwardPage)
            {
                rel += "<a href='" + cutUrl + "page=" + _forwardPage + "'>&lt;&lt;</a>&nbsp;";
            }
            else
            {
                rel += "&lt;&lt;&nbsp;";
            }
            if (cutPage < _nextPage)
            {
                rel += "<a href='" + cutUrl + "page=" + _nextPage + "'>&gt;</a>&nbsp;";
            }
            else
            {
                rel += "&lt;&lt;&nbsp;";
            }
            if (cutPage < _endPage)
            {
                rel += "<a href='" + cutUrl + "page=" + _endPage + "'>&gt;|</a>&nbsp;";
            }
            else
            {
                rel += "&gt;|&nbsp;";
            }
            rel += "" + cutPage + "Page/Total" + _totalPage + "";
            return rel;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="par"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public static int GoToValue(object par, int v)
        {
            int rel = 0;
            try
            {
                object a = par.GetType();
                if (par == null || par.ToString() == "")
                {
                    rel = v;
                }
                else
                {
                    rel = Convert.ToInt32(par);
                }
            }
            catch (Exception ex)
            {
                rel = v;
            }
            return rel;
        }
        /// <summary>
        /// 处理当前页面URI
        /// </summary>
        /// <param name="uri">当前URI地址</param>
        /// <returns>返回不含page的URl,使用时直接与page拼接</returns>
        public static string urlPage(string uri)
        {
            if (uri.Trim() == "")
            {
                return "";
            }
            else
            {
                uri = uri.Replace("&amp;", "&");
                string[] arr = uri.Split('?');
                string c1 = arr[0];
                string c2 = "";
                if (arr.Length > 1)
                {
                    c2 = arr[1];
                }
                string[] brr = c2.Split('&');
                string c4 = "";
                //var a = brr.Length;
                if (brr.Length > 0 & c2.Length > 0)
                {
                    for (int ii = 0; ii < brr.Length; ii++)
                    {
                        string c3 = brr[ii];
                        if (c3.Contains("page=") == false)
                        {
                            c4 += brr[ii] + "&amp;";
                        }
                    }
                    if (c4.Length > 5)
                        c4 = c4.Substring(0, (c4.Length - 5));
                }
                string url = "";
                if (c4 == "")
                {
                    url = c1 + "?";
                }
                else
                {
                    url = c1 + "?" + c4 + "&amp;";
                }
                return url;
            }
        }

        private static void ten(int len, int cur, int tol, out int s, out int e)
        {
            int t1 = len / 2;
            s = 0;
            e = tol;
            if (len < tol)
            {
                if (cur - t1 >= 0 && cur + t1 <= tol)
                {
                    s = cur - t1;
                    e = cur + t1;
                }
                else if (cur - t1 > 0)
                {
                    s = tol - len;
                    e = tol;
                }
                else
                {
                    s = 0;
                    e = cur + t1 + (len - cur - t1);
                }
            }
        }

    }

